package com.woniuxy.yogaapp.provider;

import com.woniuxy.yogaapp.pojo.Chat;
import org.apache.ibatis.jdbc.SQL;

/**
 * 各种信息动态sql的提供类
 */
public class ChatProvider {

    /**
     * 动态生成查询信息通知的sql
     * @param chat
     * @return
     */
    public String findChat(Chat chat) {
        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM("chat");
        if (chat.getChatid() != null && chat.getChatid() != 0) {
            sql.WHERE("chatid = #{chatid}");
        }
        if (chat.getSender() != null && chat.getSender() != 0) {
            sql.WHERE("sender = #{sender}");
        }
        if (chat.getRecipient() != null && chat.getRecipient() != 0) {
            sql.WHERE("recipient = #{recipient}");
        }
        if (chat.getMestype() != null && chat.getMestype() != 0) {
            sql.WHERE("mestype = #{mestype}");
        }
        if (chat.getState() != null && chat.getState().length() != 0) {
            sql.WHERE("state = #{state}");
        }
      
        return sql.toString();
    }

    /**
     * 动态生成修改说说状态的sql
     * @param chat
     * @return
     */
    public String updateChat(Chat chat) {
        SQL sql = new SQL();

        sql.UPDATE("chat");
        if (chat.getState() != null && chat.getState().length() != 0) {
            sql.SET("state = #{state}");
        }
        if (chat.getMestype() != null && chat.getMestype() != 0) {
            sql.SET("mestype = #{mestype}");
        }
        if (chat.getChatid() != null && chat.getChatid() != 0) {
            sql.WHERE("chatid = #{chatid}");
        }
        if (chat.getSender() != null && chat.getSender() != 0) {
            sql.WHERE("sender = #{sender}");
        }
        if (chat.getRecipient() != null && chat.getRecipient() != 0) {
            sql.WHERE("recipient = #{recipient}");
        }


        return sql.toString();
    }


    /**
     * 生成动态查询未读信息的sql
     * @param chat
     * @return
     */
    public String findUnreadInfo(Chat chat) {

        SQL sql = new SQL();
        sql.SELECT("count(*)");
        sql.FROM("chat");
        if (chat.getChatid() != null && chat.getChatid() != 0) {
            sql.WHERE("chatid = #{chatid}");
        }
        if (chat.getSender() != null && chat.getSender() != 0) {
            sql.WHERE("sender = #{sender}");
        }
        if (chat.getRecipient() != null && chat.getRecipient() != 0) {
            sql.WHERE("recipient = #{recipient}");
        }
        if (chat.getMestype() != null && chat.getMestype() != 0) {
            sql.WHERE("mestype = #{mestype}");
        }
        sql.WHERE("state = '未读'");

        return sql.toString();
    }
}
